$(function () {
  var form = layui.form
  initUser()
  let btnReset = document.querySelector('#btnReset')
  let layui_form = document.querySelector('.layui-form')
  // 重置按钮
  btnReset.addEventListener('click', function (e) {
    e.preventDefault()
    initUser()
  })
  // 更新数据
  form.on('submit(formDemo)', function (data) {
    let formData = new FormData(layui_form)
    let str = ''
    formData.forEach((value, key) => {
      str += `${key}=${value}&`
    })
    let newstr = str.slice(0,-1)
    console.log(newstr);
    axios.post('/my/userinfo', newstr)
      .then(res => {
        window.parent.location.assign('/index.html')
      })
    return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
  });
})
function initUser() {
  var form = layui.form
  var layer = layui.layer
  axios.get('/my/userinfo')
    .then(res => {
      let data = res.data.data
      if (res.data.status !== 0) {
        return layer.msg('信息获取失败');
      }
      form.val('formUserInfo', data)
    })
}